package com.mu.cloud.feign.util;

import org.dozer.DozerBeanMapper;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class DozerUtil {

    private static DozerBeanMapper mapper;

    public static  <T, S> T convert(S source, Class<T> clazz) {
        return mapper.map(source,clazz);
    }


    public static <T, S> List<T> convert(List<S> sourceList, Class<T> clazz) {
        return sourceList.stream().map(source-> mapper.map(source, clazz)).collect(Collectors.toList());
    }


    public static <T, S> Set<T> convert(Set<S> sourceSet, Class<T> clazz) {
        return sourceSet.stream().map(source-> mapper.map(source, clazz)).collect(Collectors.toSet());
    }


    public static <T, S> T[] convert(S[] s, Class<T> clazz) {
        return Arrays.stream(s).map(source -> mapper.map(source, clazz)).collect(Collectors.toList()).toArray((T[]) Array.newInstance(clazz, s.length));
    }

    public void setMapper(DozerBeanMapper mapper) {
        DozerUtil.mapper = mapper;
    }

    public DozerBeanMapper getMapper(){
        return DozerUtil.mapper;
    }
}
